package com.qf.ex;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author xiaodoudou
 * @version V1.0
 * @Project day09_05_springmvc_upload
 * @Package com.qf.ex
 * @Description:
 * @Date 2022/7/14 20:14
 * @ControllerAdvice控制器的通知对象 当控制器出现异常时会通知此对象
 *  @ExceptionHandler 标记在方法上指定该方法要处理的异常
 */
@ControllerAdvice
@RestControllerAdvice
public class MyHandleException {
    @ExceptionHandler({
            CustomerException.class
    })
    public String handlerCustomerException(CustomerException ex){

        return "自定义异常";
    }


    @ExceptionHandler({
            Exception.class
    })
    public String handlerOtherException(Exception ex){

        return "其他异常";
    }



//    预知异常
//    @ExceptionHandler({
//            CustomerException.class
//    })
//    public ModelAndView handlerCustomerException(CustomerException ex){
//        ModelAndView modelAndView=new ModelAndView();
//        modelAndView.addObject("msg","自定义异常");
//        modelAndView.setViewName("/error.jsp");
//        return modelAndView;
//    }
////    其他异常
//    @ExceptionHandler({
//            Exception.class
//    })
//    public ModelAndView handlerOtherException(Exception ex){
//        ModelAndView modelAndView=new ModelAndView();
//        modelAndView.addObject("msg","其他异常");
//        modelAndView.setViewName("/error.jsp");
//        return modelAndView;
//    }
}
